home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / PROGRAMS / CH7 / 7-6.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-09-19  |  10.9 KB  |  327 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSpreadsheet 
  3.    Caption         =   "Spreadsheet"
  4.    ClientHeight    =   1344
  5.    ClientLeft      =   168
  6.    ClientTop       =   1896
  7.    ClientWidth     =   8052
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   7.8
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    LinkTopic       =   "Form1"
  18.    PaletteMode     =   1  'UseZOrder
  19.    ScaleHeight     =   1344
  20.    ScaleWidth      =   8052
  21.    Begin VB.TextBox txtCell 
  22.       Height          =   314
  23.       Index           =   1
  24.       Left            =   396
  25.       TabIndex        =   4
  26.       Top             =   792
  27.       Width           =   1205
  28.    End
  29.    Begin VB.CommandButton cmdQuit 
  30.       Caption         =   "Quit"
  31.       BeginProperty Font 
  32.          Name            =   "MS Sans Serif"
  33.          Size            =   9.6
  34.          Charset         =   0
  35.          Weight          =   700
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   281
  41.       Left            =   4224
  42.       TabIndex        =   3
  43.       Top             =   132
  44.       Width           =   1469
  45.    End
  46.    Begin VB.CommandButton cmdNew 
  47.       Caption         =   "New"
  48.       BeginProperty Font 
  49.          Name            =   "MS Sans Serif"
  50.          Size            =   9.6
  51.          Charset         =   0
  52.          Weight          =   700
  53.          Underline       =   0   'False
  54.          Italic          =   0   'False
  55.          Strikethrough   =   0   'False
  56.       EndProperty
  57.       Height          =   281
  58.       Left            =   2376
  59.       TabIndex        =   2
  60.       Top             =   132
  61.       Width           =   1469
  62.    End
  63.    Begin VB.Label lblColLab 
  64.       Caption         =   "A"
  65.       BeginProperty Font 
  66.          Name            =   "MS Sans Serif"
  67.          Size            =   9.6
  68.          Charset         =   0
  69.          Weight          =   700
  70.          Underline       =   0   'False
  71.          Italic          =   0   'False
  72.          Strikethrough   =   0   'False
  73.       EndProperty
  74.       Height          =   281
  75.       Index           =   1
  76.       Left            =   924
  77.       TabIndex        =   1
  78.       Top             =   528
  79.       Width           =   281
  80.    End
  81.    Begin VB.Label lblRowLab 
  82.       Caption         =   "1"
  83.       BeginProperty Font 
  84.          Name            =   "MS Sans Serif"
  85.          Size            =   9.6
  86.          Charset         =   0
  87.          Weight          =   700
  88.          Underline       =   0   'False
  89.          Italic          =   0   'False
  90.          Strikethrough   =   0   'False
  91.       EndProperty
  92.       Height          =   281
  93.       Index           =   1
  94.       Left            =   0
  95.       TabIndex        =   0
  96.       Top             =   792
  97.       Width           =   281
  98.    End
  99. Attribute VB_Name = "frmSpreadsheet"
  100. Attribute VB_GlobalNameSpace = False
  101. Attribute VB_Creatable = False
  102. Attribute VB_PredeclaredId = True
  103. Attribute VB_Exposed = False
  104. Dim maxCol As Integer         'Number of columns in spreadsheet
  105. Dim maxRow As Integer         'Number of rows in spreadsheet
  106. Dim incStartRow As Integer    'Row where income categories begin
  107. Dim incStopRow As Integer     'Row where income categories end
  108. Dim incTotRow As Integer      'Row where income total is displayed
  109. Dim expStartRow As Integer    'Row where expense categories begin
  110. Dim expStopRow As Integer     'Row where expense categories end
  111. Dim expTotRow As Integer      'Row where expense total is displayed
  112. Dim balRow As Integer         'Row where balance is displayed
  113. Dim startCol As Integer       'Column where numeric data begins
  114. Dim stopCol As Integer        'Column where numeric data ends
  115. Dim totCol As Integer         'Column where total for each row is displayed
  116. 'Control Arrays
  117. 'txtCell()                     Control array for data cells
  118. 'lblRowLab()                   Control array for numeric row labels
  119. 'lblColLab()                   Control array for alphabetic column labels
  120. Private Sub cmdNew_Click()
  121.   Dim row As Integer, col As Integer
  122.   'Clear all data and total text boxes
  123.   For col = 1 To maxCol
  124.     For row = 1 To maxRow
  125.       txtCell(Indx(row, col)).Text = ""
  126.     Next row
  127.   Next col
  128.   Call SetStructure
  129.   'Place cursor in first data txtCell
  130.   txtCell(Indx(1, 1)).SetFocus
  131. End Sub
  132. Private Sub cmdQuit_Click()
  133.   End
  134. End Sub
  135. Private Sub CreateSpreadsheet()
  136.   Dim row As Integer, col As Integer, i As Integer
  137.   Dim cellHeight As Single, cellWidth As Single
  138.   Dim cellTop As Single, cellLeft As Single
  139.   cellHeight = txtCell(1).Height
  140.   cellWidth = txtCell(1).Width
  141.   'Create cells
  142.   For row = 1 To maxRow
  143.     For col = 1 To maxCol
  144.       i = Indx(row, col)
  145.       If Not (col = 1 And row = 1) Then
  146.           Load txtCell(i)
  147.       End If
  148.       If row > 1 Then
  149.           cellTop = txtCell(Indx(row - 1, col)).Top
  150.           txtCell(i).Top = cellTop + cellHeight
  151.       End If
  152.       If col > 1 Then
  153.           cellLeft = txtCell(Indx(row, col - 1)).Left
  154.           txtCell(i).Left = cellLeft + cellWidth
  155.       End If
  156.       txtCell(i).Visible = True
  157.     Next col
  158.   Next row
  159.   'Create Row Labels
  160.   For row = 2 To maxRow
  161.     Load lblRowLab(row)
  162.     lblRowLab(row).Top = lblRowLab(row - 1).Top + cellHeight
  163.     lblRowLab(row).Caption = LTrim(Str(row))
  164.     lblRowLab(row).Visible = True
  165.   Next row
  166.   'Create Column Labels
  167.   For col = 2 To maxCol
  168.     Load lblColLab(col)
  169.     lblColLab(col).Left = lblColLab(col - 1).Left + cellWidth
  170.     lblColLab(col).Caption = Chr(col + 64)
  171.     lblColLab(col).Visible = True
  172.   Next col
  173.   'Set form height and width to accommodate all objects
  174.   i = Indx(maxRow, maxCol)
  175.   frmSpreadsheet.Height = txtCell(i).Top + cellHeight + 500
  176.   frmSpreadsheet.Width = txtCell(i).Left + cellWidth + 200
  177. End Sub
  178. Private Sub DisplayTotals()
  179.   ReDim itot(startCol To stopCol) As Single
  180.   ReDim etot(startCol To stopCol) As Single
  181.   'Calculate and show totals for Income each quarter
  182.   Call TotalIncome(itot())
  183.   'Calculate and show totals for Expenses each quarter
  184.   Call TotalExpenses(etot())
  185.   'Calculate and show Balances for each quarter
  186.   Call ShowBalances(itot(), etot())
  187.   'Calculate and show the Total of each Income & Expense category
  188.   Call TotalRows
  189.   'Calculate and show grand totals of quarter totals and balances
  190.   Call ShowGrandTotals(itot(), etot())
  191. End Sub
  192. Private Sub Form_Load()
  193.   'Establish number of rows and columns. Trial and error show
  194.   'that a maximum of 20 rows and 8 columns will fit the screen.
  195.   'For this particular application, 16 rows and 6 columns are adequate.
  196.   maxRow = 16
  197.   maxCol = 6
  198.   Call CreateSpreadsheet
  199.   Call SetStructure
  200.   Call SetDefaults
  201. End Sub
  202. Private Function Indx(row As Integer, col As Integer) As Integer
  203.   Indx = (row - 1) * maxCol + col
  204. End Function
  205. Private Sub SetDefaults()
  206.   'Set default values specific to this application
  207.   txtCell(Indx(3, 1)).Text = "Job"
  208.   txtCell(Indx(4, 1)).Text = "Parents"
  209.   txtCell(Indx(5, 1)).Text = "Scholarship"
  210.   txtCell(Indx(9, 1)).Text = "Tuition"
  211.   txtCell(Indx(10, 1)).Text = "Food"
  212.   txtCell(Indx(11, 1)).Text = "Rent"
  213.   txtCell(Indx(12, 1)).Text = "Books"
  214.   txtCell(Indx(13, 1)).Text = "Misc"
  215. End Sub
  216. Private Sub SetStructure()
  217.   txtCell(Indx(1, 2)).Text = "Fall"
  218.   txtCell(Indx(1, 3)).Text = "Winter"
  219.   txtCell(Indx(1, 4)).Text = "Spring"
  220.   txtCell(Indx(1, 5)).Text = "Summer"
  221.   txtCell(Indx(1, 6)).Text = "Total"
  222.   txtCell(Indx(1, 6)).ForeColor = vbGreen
  223.   txtCell(Indx(2, 1)).Text = "Income"
  224.   txtCell(Indx(2, 1)).ForeColor = vbMagenta
  225.   txtCell(Indx(6, 1)).Text = "Total"
  226.   txtCell(Indx(6, 1)).ForeColor = vbGreen
  227.   txtCell(Indx(8, 1)).Text = "Expenses"
  228.   txtCell(Indx(8, 1)).ForeColor = vbMagenta
  229.   txtCell(Indx(14, 1)).Text = "Total"
  230.   txtCell(Indx(14, 1)).ForeColor = vbGreen
  231.   txtCell(Indx(16, 1)).Text = "Balance"
  232.   txtCell(Indx(16, 1)).ForeColor = vbGreen
  233.   incStartRow = 3
  234.   incStopRow = 5
  235.   incTotRow = 6
  236.   expStartRow = 9
  237.   expStopRow = 13
  238.   expTotRow = 14
  239.   balRow = 16
  240.   startCol = 2
  241.   stopCol = 5
  242.   totCol = 6
  243. End Sub
  244. Private Sub ShowBalances(itot() As Single, etot() As Single)
  245.   Dim col As Integer
  246.   For col = startCol To stopCol
  247.     txtCell(Indx(balRow, col)).Text = FormatNumber(itot(col) - etot(col), 0)
  248.   Next col
  249. End Sub
  250. Private Sub ShowGrandTotals(itot() As Single, etot() As Single)
  251.   Dim col As Integer, iTotal As Single, eTotal As Single
  252.   'Compute and display grand totals for income, expenses, and balance
  253.   iTotal = 0
  254.   eTotal = 0
  255.   For col = startCol To stopCol
  256.     iTotal = iTotal + itot(col)
  257.     eTotal = eTotal + etot(col)
  258.   Next col
  259.   txtCell(Indx(incTotRow, totCol)) = FormatNumber(iTotal, 0)
  260.   txtCell(Indx(expTotRow, totCol)) = FormatNumber(eTotal, 0)
  261.   txtCell(Indx(balRow, totCol)) = FormatNumber(iTotal - eTotal, 0)
  262. End Sub
  263. Private Sub TotalExpenses(etot() As Single)
  264.   Dim row As Integer, col As Integer
  265.   'Total expenses for each of four quarters
  266.   For col = startCol To stopCol
  267.     etot(col) = 0
  268.     For row = expStartRow To expStopRow
  269.       etot(col) = etot(col) + Val(txtCell(Indx(row, col)).Text)
  270.     Next row
  271.     txtCell(Indx(expTotRow, col)).Text = FormatNumber(etot(col), 0)
  272.   Next col
  273. End Sub
  274. Private Sub TotalIncome(itot() As Single)
  275.   Dim row As Integer, col As Integer
  276.   'Total income for each of four quarters
  277.   For col = startCol To stopCol
  278.     itot(col) = 0
  279.     For row = incStartRow To incStopRow
  280.       itot(col) = itot(col) + Val(txtCell(Indx(row, col)).Text)
  281.     Next row
  282.     txtCell(Indx(incTotRow, col)).Text = FormatNumber(itot(col), 0)
  283.   Next col
  284. End Sub
  285. Private Sub TotalRows()
  286.   Dim row As Integer, col As Integer, rowTot As Single
  287.   'Total each income category
  288.   For row = incStartRow To incStopRow
  289.     rowTot = 0
  290.     For col = startCol To stopCol
  291.       rowTot = rowTot + Val(txtCell(Indx(row, col)).Text)
  292.     Next col
  293.     txtCell(Indx(row, totCol)).Text = FormatNumber(rowTot, 0)
  294.   Next row
  295.   'Total each expense category
  296.   For row = expStartRow To expStopRow
  297.     rowTot = 0
  298.     For col = startCol To stopCol
  299.       rowTot = rowTot + Val(txtCell(Indx(row, col)).Text)
  300.     Next col
  301.     txtCell(Indx(row, totCol)).Text = FormatNumber(rowTot, 0)
  302.   Next row
  303. End Sub
  304. Private Sub txtCell_GotFocus(Index As Integer)
  305.   Dim row As Integer, col As Integer
  306.   'Force focus into a data txtCell for this application
  307.   row = Int((Index - 1) / maxCol) + 1
  308.   col = ((Index - 1) Mod maxCol) + 1
  309.   If col > stopCol Then
  310.       row = row + 1
  311.       col = startCol
  312.   End If
  313.   If row < incStartRow Then
  314.       row = incStartRow
  315.     ElseIf (row > incStopRow) And (row < expStartRow) Then
  316.       row = expStartRow
  317.     ElseIf row > expStopRow Then
  318.       row = incStartRow
  319.   End If
  320.   If Indx(row, col) <> Index Then
  321.       txtCell(Indx(row, col)).SetFocus
  322.   End If
  323. End Sub
  324. Private Sub txtCell_LostFocus(Indx As Integer)
  325.   Call DisplayTotals
  326. End Sub
  327.